package org.execute.tool.多个数据库链接;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;

public class DatabaseUtil {
    
    public static DataSource getDataSource(Archive archive) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(archive.getUrl());
        config.setUsername(archive.getUsername());
        config.setPassword(archive.getPassword());
        config.setDriverClassName(archive.getDriverClass());

        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        HikariDataSource dataSource = new HikariDataSource(config);
        return dataSource;
    }
    public static HashMap<Integer, DataSource> getDataSource(List<Archive> archiveList) {
        HashMap<Integer, DataSource> stringDataSourceHashMap = new HashMap<>();
        for (Archive archive : archiveList) {

            DataSource dataSource = getDataSource(archive);
            stringDataSourceHashMap.put(archive.getId(),dataSource);
        }
        return stringDataSourceHashMap;
    }

}